// pages/clock/clock.js
import __config from '../../config/env.js'
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk')
var qqmapsdk = new QQMapWX({
  key: 'VYSBZ-K273P-GEQD3-LLP4A-KATX2-SIBXR' // 必填
})
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:__config.basePath,
    storeid:'',
    dis:'',
    jinDu:0,
    weiDu:0,
    title:'拜访',
    progress:-1,
    camaraType:'',
    shop:{},
    src:{},
    imageurl:'',
    remarks:'',
    TabCur:0,
    myLocation:'',
    itude:'',
    gid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      gid:options.gid
    })
    this.getMyLocation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })
      }
    })
  },
  imgUpload(url){
    this.setData({
      imageurl:url
    })
  },
  getMyLocation(){
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success:(res) =>{
        let obj = {latitude:res.latitude,longitude:res.longitude}
        that.setData({
          jinDu:res.latitude,
          weiDu:res.longitude,
          itude:JSON.stringify(obj)
        })
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: (res) => {
            that.setData({
              myLocation: res.result.address
            })
          }
        })
      },
      fail : res => {
        if('getLocation:fail auth deny' == res.errMsg){
          wx.showModal({
            title: '提示',
            content: '为获取用户定位授权',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
         }else if('getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' == res.errMsg){
          wx.showModal({
            title: '提示',
            content: '请打开手机定位',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }else{
          wx.showToast({
            title: res.errMsg,
          })
        }
       }
    })
  },
  getTaskDetail(){
    app.api.getTempVisits().then(res =>{
      console.log(res)
      this.setData({
        shop:res.rows
      })
      this.setData({
        imageurl:res.rows.imageurl || '',
        signimage2:res.rows.signimage2 || '',
        signOutimage:res.rows.signoutimage1 || '',
        remarks:''
      })
      this.setData({
        progress:res.rows.progress || -1
      })
    })
  },
  openCanmera(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      camaraType:type
    })
    wx.navigateTo({
      url: '/pages/takePhoto/takePhoto'
    })
  },
  addImg(img){
    this.imgUpload(img)
  },
  DelImg(e){
    let type = e.currentTarget.dataset.type
    if(type== 'imageurl'){
      this.setData({
        imageurl:''
      })
    }else if(type == 'signimage2'){
        this.setData({
          signimage2:''
      })
    }else if(type == 'signOutimage'){
      this.setData({
        signOutimage:''
      })
    }
  },
  signIn(){
    if(this.data.jinDu == 0){
      wx.showModal({
        title: '提示',
        content: '获取经纬度失败',
        showCancel:false,
        success:res =>{
          wx.navigateBack()
        }
      })
    }
    else if(this.data.imageurl == ''){
      wx.showModal({
        title: '提示',
        content: '考勤照片必填',
        showCancel:false
      })
    }
    else if(this.data.remarks == ''){
      wx.showModal({
        title: '提示',
        content: '备注必填',
        showCancel:false
      })
    }
    else{
      let obj = {}
      obj.itude = this.data.itude
      obj.remarks = this.data.remarks
      obj.address = this.data.myLocation
      app.api.imgUpload(this.data.imageurl).then(res => {
        obj.imageurl = res.imageurl
        app.api.saveClocksignin(obj).then(res =>{
          if(res.code == 200){
            this.setData({
              title:'拜访',
            })
            app.dialog.success(res.info)
          }
        })
      })
    }
  },
  signOut(){
    let obj = {}
    console.log(this.data.jinDu)
    if(this.data.jinDu == 0){
      wx.showModal({
        title: '提示',
        content: '获取经纬度失败',
        showCancel:false,
        success:res =>{
          wx.navigateBack()
        }
      })
    }else if(this.data.imageurl == ''){
      wx.showModal({
        title: '提示',
        content: '考勤照片必填',
        showCancel:false
      })
    }else{
      let obj = {}
      obj.itude = this.data.itude
      obj.remarks = this.data.remarks
      obj.address = this.data.myLocation
      app.api.imgUpload(this.data.imageurl).then(res => {
        obj.imageurl = res.imageurl
        app.api.saveClocksignout(obj).then(res =>{
          if(res.code == 200){
            app.dialog.success(res.info)
            wx.navigateBack();
          }
        })
      })
    }
  },
  ViewImage(e){
    let url = e.currentTarget.dataset.url
    console.log(url)
    wx.previewImage({
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  getDisance(la1, lo1, la2, lo2) {
    let La1 = la1 * Math.PI / 180.0;
    let La2 = la2 * Math.PI / 180.0;
    let La3 = La1 - La2;
    let Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2);
    return s;
  },
  toSignIn(){
    if(-1 == this.data.progress){
      this.setData({
        title:'签到'
      })
    }else if(0 == this.data.progress){
      wx.showModal({
        title: '提示',
        content: '已经签到',
        showCancel:false,
        success:res =>{
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '已经签退',
        showCancel:false,
        success:res =>{
        }
      })
   }
  },
  toSignOut(){
    if(0 == this.data.progress){
      this.setData({
        title:'签退'
      })
    }else if(-1 == this.data.progress){
      wx.showModal({
        title: '提示',
        content: '请先签到',
        showCancel:false,
        success:res =>{
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '已经签退',
        showCancel:false,
        success:res =>{
        }
      })
    }
  },
  iptRemarks:function(e){
    this.setData({
      remarks:e.detail.value
    })
  }
})